<template>
  <div>
    <div class="printOrder">
      <el-row>
        <el-col :span="24">
          <label class="durg_title">{{ typeName }}详情</label>
        </el-col>
      </el-row>
      <!-- 入库单号： 4432 发货日期： 入库日期： 2019-08-02 入库方式： 采购入库 供应商： 和平门诊 备注： 制单时间： 2019-08-02 15:03:44 制单人： 李军
      审核日期： 2019-08-02 审核人员： 李军-->
      <el-row>
        <el-col :span="4">
          <label>{{ typeName }}单号：{{ detail.stockCode }}</label>
        </el-col>
        <el-col :span="4">
          <label
            >{{ typeName }}日期：{{
              detail.putInStorageDate | changeTime
            }}</label
          >
        </el-col>
        <el-col :span="4">
          <label
            >{{ typeName }}方式：{{
              detail.inOutWay | changeAssist(wayList)
            }}</label
          >
        </el-col>
        <el-col :span="4">
          <label>供应商：{{ detail.supplierName }}</label>
        </el-col>
        <el-col :span="4">
          <label>备注：{{ detail.remark }}</label>
        </el-col>
      </el-row>
      <el-row style="margin-top:10px">
        <el-col v-if="detail.examineTime" :span="4">
          <label>审核日期：{{ detail.examineTime | changeTime }}</label>
        </el-col>
        <el-col :span="4">
          <label>制单人： {{ detail.examineName }}</label>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <table class="table-box">
            <thead>
              <tr>
                <th>序号</th>
                <th>商品名称</th>
                <th>单位</th>
                <th>生产厂商</th>
                <th>数量</th>
                <th>零售价（￥）</th>
                <th>成本价（￥）</th>
                <th>成本合计（￥）</th>
                <th>批号</th>
                <th>有效期</th>
                <th>发票号</th>
                <th>验收结论</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item, index) in itemList" :key="index">
                <td>{{ index + 1 }}</td>
                <td>{{ item.sourceName }}</td>
                <td>{{ item.packUnit | changeAssist(dwList) }}</td>
                <td>{{ item.factory }}</td>
                <td>{{ item.count }}</td>
                <td>￥{{ item.retailPrice }}</td>
                <td>￥{{ item.purchasePrice }}</td>
                <td>￥{{ item.totalMoney }}</td>
                <td>{{ item.lotNumber }}</td>
                <td>{{ item.effectiveTime | changeTime }}</td>
                <td>{{ item.invoiceNumber }}</td>
                <td>
                  {{ item.acceptanceConclusion == 1 ? "合格" : "不合格" }}
                </td>
              </tr>
              <tr>
                <td colspan="7">合计成本</td>
                <td colspan="5">￥{{ totalMoney }}</td>
                <!-- <td colspan="7"></td> -->
              </tr>
            </tbody>
          </table>
        </el-col>
      </el-row>
    </div>
    <br />
    <span slot="footer">
      <el-button v-if="detail.status != 1" type="primary" @click="onExamine"
        >审核</el-button
      >
      <el-button @click="onClose">返回</el-button>
      <el-button @click="doPrint" style="float:right;">
        <i class="iconfont icondayin"></i>打印
      </el-button>
    </span>
    <iframe
      v-if="showOrder"
      frameborder="0"
      height="0px"
      width="0px"
      ref="iframe"
      src="/print/storage.html"
    ></iframe>
  </div>
</template>
<script>
import Valid from "@/_js/validate.js";
export default {
  data() {
    return {
      btnLoading: false,
      showOrder: false,
      iframeContent: {},
      detail: "",
      typeName: "",
      itemList: [],
      totalMoney: "",
      wayList: [],
      dwList: [],
    };
  },
  mounted() {
    let th = this;
    th.verifyUrlAttack(th.$route.query).then(() => {
      th.durg_id = th.jieMi(th.$route.query.id);
      th.typeName = th.$route.query.type == 1 ? "入库" : "出库";
      th.onOpen();
      th.getBasicData();
    });
  },
  methods: {
    onOpen() {
      let th = this;
      if (th.$route.query.type == 2) {
        th.$api.getPutDetailV2({ outPutId: th.durg_id }).then((res) => {
          if (res.executed) {
            th.detail = res.data.medicalStockOutPutDisplayVO;
            let info = res.data.medicalStockOutPutDisplayVO;
            th.detail.examineName = info.createUserName;
            th.detail.inOutWay = info.outWay;
            th.detail.putInStorageDate = info.outDate;
            th.itemList = res.data.medicalStockOutPutDetailDisplayVOS;
            let totalMoney = 0;
            if (th.itemList.length > 0) {
              th.itemList.forEach((it) => {
                it.totalMoney = Number(it.purchasePrice) * it.outQuantity;
                it.totalMoney = Number(it.totalMoney).toFixed(2);
                it.sourceName = it.goodsName;
                it.packUnit = it.outQuantityUnit;
                it.count = it.outQuantity;
                totalMoney += Number(it.purchasePrice) * it.outQuantity;
              });
              totalMoney = Number(totalMoney).toFixed(2);
              th.totalMoney = totalMoney;
            }
          }
        });
      } else {
        th.$api.findMedicalStockByIdV2({ id: th.durg_id }).then((res) => {
          if (res.executed) {
            th.detail = res.data.medicalStockInfo;
            th.itemList = res.data.itemDtoList;
            let totalMoney = 0;
            if (th.itemList.length > 0) {
              th.itemList.forEach((it) => {
                totalMoney += Number(it.purchasePrice) * it.count;
                it.totalMoney = Number(it.totalMoney).toFixed(2);
                it.totalMoney = Number(it.purchasePrice) * it.count;
              });
              totalMoney = Number(totalMoney).toFixed(2);
              th.totalMoney = totalMoney;
            }
          }
        });
      }
    },
    onClose() {
      let th = this;
      let type = th.$route.query.type;
      if (type == 1) {
        th.addNavMenu("物资管理", "/page/suppliesIndex");
        th.$router.push({
          path: "/page/suppliesIndex",
          query: th.getUrlToken({ id: th.jiaMi(1) }),
        });
      } else {
        th.addNavMenu("物资管理", "/page/suppliesIndex");
        th.$router.push({
          path: "/page/suppliesIndex",
        });
      }
    },
    //获取通用配置
    getBasicData() {
      if (this.$route.query.type == 1) {
        this.getCommonConfigByKey("入库方式").then((res) => {
          this.wayList = res;
        });
      } else {
         this.getCommonConfigByKey("出库方式").then((res) => {
          this.wayList = res;
        });
      }
      this.getCommonConfigByKey("单位").then((res) => {
        this.dwList = res;
      });
    },
    onExamine() {
      let th = this;
      th.$confirm(`确定审核通过吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      }).then(() => {
        let put = function() {
          let j = { outPutId: th.durg_id };
          th.showLoading();
          th.$api
            .examinePutStorge(j)
            .then((res) => {
              if (res.executed) {
                th.$message.success("审核成功");
                th.onClose();
              } else {
                th.$message.error(res.message);
              }
              th.showLoading(false);
            })
            .catch((error) => {
              th.showLoading(false);
            });
        };
        let rut = function() {
          let j = { id: th.durg_id };
          th.showLoading();
          th.$api
            .examineStockV2(j)
            .then((res) => {
              if (res.executed) {
                th.$message.success("审核成功");
                th.onClose();
              } else {
                th.$message.error(res.message);
              }
              th.showLoading(false);
            })
            .catch((error) => {
              th.showLoading(false);
            });
        };
        if (th.$route.query.type == 2) {
          put();
        } else {
          rut();
        }
      });
    },
    //打印
    doPrint() {
      let th = this;
      th.showOrder = true;
      th.showLoading();
      setTimeout(function() {
        th.iframeContent = th.$refs.iframe.contentWindow;
        th.iframeContent.postMessage(
          {
            cmd: "postJson",
            params: JSON.stringify({
              detail: th.detail,
              list: th.itemList,
              hospitalName: th.adminUser.hospitalName,
              logo: th.adminUser.printLogo,
              wayList: th.wayList,
              dwList: th.dwList,
              type: th.$route.query.type
            }),
          },
          "*"
        );
      }, 1000);
      setTimeout(function() {
        th.showLoading(false);
      }, 1000);
    },
  },
  components: {},
};
</script>
<style lang="scss" scoped>
.durg_title {
  display: inline-block;
  width: 100%;
  font-size: 16px;
  border-bottom: 1px solid #ddd;
  padding-bottom: 10px;
  margin-bottom: 10px;
}
.durg_form {
  .el-input,
  .el-select {
    width: 90%;
  }
  .el-form-item__label {
    padding-bottom: 0;
  }
  .one_dose_select {
    width: 30%;
    position: absolute;
    right: 28px;
  }
}
</style>
